package com.ebc.eservice.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author: Joey
 * @Description: security的user类
 * @date:2022/9/19 0:52
 */
@ApiModel(value="UserService对象", description="")
@Data
@NoArgsConstructor
public class UserDT implements UserDetails {
    private EbcUser user;
    private List<EbcRole> roles;
    private String permissions; //单独抽离权限名字

    public UserDT(EbcUser user, List<EbcRole> roles) {
        this.user = user;
        this.roles = roles;
    }

    @JSONField(serialize = false)
    private Collection<SimpleGrantedAuthority> authorities;

//    List<GrantedAuthority> authorities= new ArrayList<GrantedAuthority>();
//				authorities.add(new SimpleGrantedAuthority("auth"));

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        authorities = new ArrayList<>();
        for(EbcRole role : roles){
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleCode()));
        }
        return authorities;
    }


    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
